<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>SM2</title>
    <script src="./lib/crypto-js.js"></script>
    <script src="./sm2.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
</head>

<body>
    <h1>SM2</h1>
    <div class="toolbar">
        <input type="text" value="hello world" placeholder="请输入当前要加密的文本" id="encode-text" />
        <button onclick="generateCipher()">生成 1 条密文</button>
        <button onclick="clearCipher()">清空密文</button>
    </div>

    <ul id="cipher-items" style="font-size: 14px;">
    </ul>

    <script>
        var pubkeyHex =
            "04510389b3e8dccc49b4749a495f9a2a7b5b1c4b4edfb98c64aa00c18d74f7f1a932b2b32a6fe5359e86a6e54b6184e269d8a4201e85a5cae7710d2f6a7b52dc88";
        var keyCollection = [];
        var $cipherItems = $('#cipher-items');
        var $msg = $('#encode-text');
        
        function generateCipher() {
            var item = '';
            var msg = $msg.val();

            for (var i = 0, len = 1; i < len; i++) {
                var encryptData = sm2Encrypt(msg, pubkeyHex, 0);

                keyCollection.push(encryptData);
                item += `<li class="cipher-items-item">{SM2}${encryptData}</li>`
            }

            $cipherItems.html($cipherItems.html() + item);
        }

        function clearCipher() {
            keyCollection = [];
            $cipherItems.html();
        }
    </script>
</body>

</html>